﻿using System;
using System.Collections.Generic;
using System.Linq;
using MathService.DSP.Signals;

namespace MathService.DSP
{
    [Serializable]
    public class CKIH : DigitalFilter, IFilter
    {
        private readonly double[] f_h;
        private readonly Queue<double> f_Data;

        public int Length { get { return f_h.Length; } }

        public double State
        {
            get
            {
                var lv_Data = f_Data.ToArray();
                return lv_Data.Select((t, i) => f_h[i] * lv_Data[lv_Data.Length - i - 1]).Sum();
            }
        }

        public CKIH(double[] h)
            : base(h.Length)
        {
            f_h = new double[h.Length];
            Array.Copy(h, f_h, h.Length);
            f_Data = new Queue<double>(h.Length);
        }

        public override double Process(double Value)
        {
            f_Data.Enqueue(Value);
            if(f_Data.Count > f_h.Length) f_Data.Dequeue();
            return State;
        }

        public override Complex TransferFunction(double f, double dt)
        {
            throw new NotImplementedException();
        }

        public new double[] ProcessSignal(double[] Signal)
        {
            return Array.ConvertAll(Signal, Process);
        }

        public new DigitalSignal ProcessSignal(DigitalSignal s)
        {
            return new DigitalSignal(s.dx, ProcessSignal(s));
        }
    }
}
